package com.qf.web.sys_menu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.utils.ResultUtil;
import com.qf.utils.ResultVo;
import com.qf.web.sys_menu.entity.Menu;
import com.qf.web.sys_menu.servie.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xufuhan
 * @Date 2024/9/10 20:34
 */

@RestController
@RequestMapping("/api/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;


    @PostMapping
    public ResultVo addMenu(@RequestBody Menu menu){
        boolean save = menuService.save(menu);
        if (save){
             return ResultUtil.success("新增菜单成功");
        }
        return ResultUtil.error("新增菜单失败");
    }

    @PutMapping
    public ResultVo editMenu(@RequestBody Menu menu){
        boolean save = menuService.updateById(menu);
        if (save){
            return ResultUtil.success("编辑菜单成功");
        }
        return ResultUtil.error("编辑菜单失败");
    }

    @DeleteMapping("/{menuId}")
    public ResultVo deleteMenu(@PathVariable("menuId") Long menuId){
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(Menu::getMenuId,menuId);
        List<Menu> list = menuService.list(menuService.query());
        if (list.size()>0){
            return ResultUtil.error("该菜单存在下级，不允许先删除");
        }
        boolean remove = menuService.removeById(menuId);
        if (remove){
            return ResultUtil.success("删除菜单成功");
        }
        return ResultUtil.error("删除菜单失败");
    }

    @GetMapping("/list")
    public ResultVo getMenuList(){
        List<Menu> menuList = menuService.getMenuList();
        return ResultUtil.success("查询成功",menuList);
    }

    //上级菜单列表
    @GetMapping("/parent")
    public ResultVo getParentList(){
        List<Menu> menuList = menuService.getParentList();
        return ResultUtil.success("查询成功",menuList);
    }
}
